import Koa from 'koa';
import path from 'path';
import Router from 'koa-router';
import { errLog } from '../log';
import { IPresetDefinedData } from '../../type';
import { packageRouter, packageResponse } from '../util';

// __dirname 是 service.js 脚本所在目录
const mainDirAtPath = path.resolve(__dirname, '..', '..');

function useRouter(router: Router, app: Koa): void {
    // ...
    const route = packageRouter(router, app);

    /**
     * 获取程序预设定义值
     */
    route('post', '/getAppPresetDefinedData', async ({ response }) => {
        try {
            const assetsDirPath = path.join(mainDirAtPath, 'assets');

            const result: IPresetDefinedData = {
                assetsDirPath: {
                    path: assetsDirPath,
                    desp: '程序公共静态资源目录路径',
                },
            };

            packageResponse(response, result);

            // ...
        } catch (error) {
            errLog(error);
            packageResponse(response, null, 0, 500, JSON.stringify(error));
        }
    });
}

export default useRouter;
